import 'package:film/controller/auth_controller.dart';
import 'package:film/service/api_service.dart';
import 'package:get/get.dart';

/// 修改密码页面控制器
class UpdatePasswordController extends GetxController {
  DioService dioService = new DioService();
  AuthController authController = Get.put(AuthController());

  /// 原密码
  var oldPassword = ''.obs;

  /// 新密码
  var newPassword = ''.obs;

  /// 确认新密码
  var confirmPassword = ''.obs;

  /// 密码可见性
  var oldPwdVisible = false.obs;
  var newPwdVisible = false.obs;
  var confirmPwdVisible = false.obs;

  /// 提交修改密码
  void submit() async {
    if (oldPassword.value.isEmpty || newPassword.value.isEmpty || confirmPassword.value.isEmpty) {
      Get.snackbar('提示', '请填写所有字段');
      return;
    }
    if (newPassword.value != confirmPassword.value) {
      Get.snackbar('提示', '两次输入的新密码不一致');
      return;
    }
    final ApiResponse<void> response = await dioService.updatePassword(
      email: authController.userInfo.value.email ?? '',
      oldPassword: oldPassword.value,
      newPassword: newPassword.value,
    );
    if (response.success) {
      Get.snackbar(
        '成功',
        '修改成功！',
        duration: Duration(seconds: 1),
      );
    }
    // 清空输入
    oldPassword.value = '';
    newPassword.value = '';
    confirmPassword.value = '';
  }
}
